"use strict";

const BaseModel = require("../basics/mysql/mapper");

class HomeModel extends BaseModel {
  constructor(cloud) {
    super(cloud, "task_record");
  }

  /**
   * 获取实例
   * @param {*} cloud 
   */
  static getInstant(cloud) {
    return new HomeModel(cloud);
  }


  /**
   * 查询单条
   * @param {*} openId 
   * @param {*} today 
   * @param {*} code 
   */
  async selectOne(openId, today, code) {
    const ex = this.createExample();
    ex.criteria.andEqual("openId", openId).andEqual("today", today).andEqual("code", code);
    return await this.selectOneByExample(ex);
  }


  /**
   * 查询列表
   * @param {*} openId 
   * @param {*} today 
   */
  async selectList(openId, today) {
    const ex = this.createExample();
    ex.criteria.andEqual("openId", openId).andEqual("today", today);
    return await this.selectListByExample(ex);
  }


  /**
   * 完成加购
   * @param {*} id 
   */
  async finishPurch(id, details) {
    const sql = `update task_record set status=1,details='${details}' where id=${id} and status=0`;
    const data = await this.mfSql(sql, []);
    return data.affectedRows;
  }

}

module.exports = HomeModel;